
/****** Object:  Stored Procedure [sapro].GiaoDichGetList    Script Date: Thursday, March 18, 2010 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[spGiaoDichGetList]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [sapro].[spGiaoDichGetList]
GO

	
			
	
	
/******************************************************************************
**		File: 
**		Name: [sapro].spGiaoDichGetList
**		Desc: 
**
**		Return values:
** 
**		Called by:   
**              
**		Parameters:
**		Input							Output
**     ----------							-----------
**
**		Auth: CodeSmith
**		Date: 3/18/2010 5:04:09 PM
*******************************************************************************
**		Change History
*******************************************************************************
**		Date:		Author:				Description:
**		--------		--------				-------------------------------------------
**    
*******************************************************************************/
CREATE PROCEDURE [sapro].spGiaoDichGetList
	@OrderBy varchar(50),
	@OrderDirection varchar(5),
	@Page int,
	@PageSize int,
	@TotalRecords int output
AS

SET TRANSACTION ISOLATION LEVEL READ COMMITTED

CREATE TABLE #TempGiaoDich (
	RowNumber INT IDENTITY (1, 1) NOT NULL,
	Mobile nvarchar(20),	
	NgayGD datetime,	
	GioGD datetime	
)

DECLARE @sql nvarchar(2000)
DECLARE @Top int

SET @Top = @Page*@PageSize
IF @PageSize > 0
   SET ROWCOUNT @Top
-- insert primary keys into temp table
SET @sql =	N'INSERT INTO #TempGiaoDich ([Mobile],[NgayGD],[GioGD]) SELECT '
SET @sql = @sql + ' [Mobile],[NgayGD],[GioGD] FROM [sapro].[GiaoDich]  ORDER BY [' + @OrderBy + N'] ' + @OrderDirection
EXEC (@sql)
SET ROWCOUNT 0

SELECT @TotalRecords = COUNT(*) FROM [GiaoDich]

SELECT
	[sapro].[GiaoDich].[Mobile],
	[sapro].[GiaoDich].[NgayGD],
	[sapro].[GiaoDich].[GioGD],
	[SoDiemTruoc],
	[SoDiemGD],
	[SoDiemSau],
	[NoiDung]
FROM
	#TempGiaoDich AS tblTemp JOIN [sapro].[GiaoDich] ON
	tblTemp.Mobile = [sapro].[GiaoDich].Mobile  AND 	
	tblTemp.NgayGD = [sapro].[GiaoDich].NgayGD  AND 	
	tblTemp.GioGD = [sapro].[GiaoDich].GioGD 	
WHERE (@PageSize = 0) OR (@PageSize > 0 AND (@Page - 1)*@PageSize < RowNumber AND RowNumber <= @Page*@PageSize)
ORDER BY RowNumber

DROP TABLE #TempGiaoDich

GO

	

